
public class BinarSortTest {

	public static void main(String[] args) {
		
		int[] array = {-2,4,12,3,2};
		for (int i=0;i<5;i++)
		{
			System.out.print(array[i] + " ");
		}
		System.out.print("\r\n");
		binarInsertsSort(array,5);
		for (int i=0;i<5;i++)
		{
			System.out.print(array[i] + " ");
		}
		System.out.print("\r\n");
	}
	
	public static void binarInsertsSort(int array[], int n){
			int x;
		    int left;
		    int right;
		    int sred;
		    for (short i = 1;  i < n; i++) 
		        if (array[i-1] > array[i]){
		            x = array[i];
		            left = 0;  
		            right = i-1;  
		            do {
		                sred = (left + right)/2;    
		                if  (array[sred] < x ) left = sred + 1;  
		                else  right = sred - 1;      
		            } while (left <= right);
		            for (int j = i-1; j>=left; j--)
		                array[j+1] = array[j];  
		            array[left] = x;
		      }
		}
	}
